package com.xiaogao.serviceEdu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.xiaogao.serviceBase.utils.exceptionHandler.XiaoGaoException;
import com.xiaogao.serviceEdu.entity.EduSubject;
import com.xiaogao.serviceEdu.entity.excel.SubjectData;
import com.xiaogao.serviceEdu.entity.vo.SubjectVO;
import com.xiaogao.serviceEdu.listener.SubjectExcelListener;
import com.xiaogao.serviceEdu.mapper.EduSubjectMapper;
import com.xiaogao.serviceEdu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 小高
 * @since 2021-06-06
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 添加课程分类
     * @param file
     */
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            // 获取文件输入流
            InputStream in = file.getInputStream();

            // 这里需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new XiaoGaoException(20001,"添加课程分类失败");
        }
    }

    /**
     * 课程分类列表（树形）
     * @return
     */
    @Override
    public List<SubjectVO> getAllSubject() {
        // stream流方法

        // 查询所有数据
        List<EduSubject> subjectList = this.list(null);

        // 把subjectList数组封装成subjectVOS
        List<SubjectVO> subjectVOS = subjectList.stream().map(subject -> {
            SubjectVO subjectVO = new SubjectVO();
            // 使用spring工具类BeanUtils，将EduSubject中的数据复制到SubjectVO中
            BeanUtils.copyProperties(subject,subjectVO);
            return subjectVO;
        }).collect(Collectors.toList());

        // filter是java8的写法，取得所有parentId为0的数据，即一级目录
        List<SubjectVO> list = subjectVOS.stream().filter(subjectVO ->
            subjectVO.getParentId().equals("0")
        ).map(menu -> { // map也是java8的写法，用于封装数据，取得他的孩子（也就是下级目录）的数据
            menu.setChildren(getChildrenData(menu,subjectVOS));
            return menu;
        }).collect(Collectors.toList());
        return list;
    }

    // 获取孩子（二级目录）的方法，递归实现
    private List<SubjectVO> getChildrenData(SubjectVO one,List<SubjectVO> two){
        List<SubjectVO> children = two.stream().filter(subjectVO ->
            subjectVO.getParentId().equals(one.getId())
        ).map(subjectVO -> {
            subjectVO.setChildren(getChildrenData(subjectVO,two));
            return subjectVO;
        }).collect(Collectors.toList());
        return children;
    }


        // 普通方法

        /*// 创建List集合，用于存储最终封装的数据列表
        List<OneSubject> finalSubjectsList = new ArrayList<>();

        // 1、查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        // 2、查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        // 3、封装一级分类
        *//* 查询出来所有的一级分类list集合遍历，得到每个一级分类对象并获取对象值
           封装到要求的list集合中：List<OneSubject> finalSubjectsList *//*
        int oneSize = oneSubjectList.size();
        for (int i = 0; i < oneSize; i++) {
            // 得到oneSubjectList每个eduSubject对象
            EduSubject eduSubject = oneSubjectList.get(i);
            // 把eduSubject里面的值获取出来，放到OneSubject对象中，
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            // spring提供工具类：BeanUtils    copyProperties方法：将eduSubject对象中内容复制到oneSubject对象中（同上面操作是一样的）
            BeanUtils.copyProperties(eduSubject,oneSubject);
            // 再把多个OneSubject放到finalSubjectsList中
            finalSubjectsList.add(oneSubject);

            // 4、封装二级分类
            // 创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectsList = new ArrayList<>();
            int twoSize = twoSubjectList.size();
            // 在一级分类循环中遍历所有的二级分类
            for (int j = 0; j < twoSize; j++) {
                // 获取每个二级分类
                EduSubject subject2 = twoSubjectList.get(j);
                // 判断二级分类parent_id和一级分类id是否一样
                if (subject2.getParentId().equals(eduSubject.getId())){
                    // 把subject2中的值复制到TwoSubject中
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subject2,twoSubject);
                    // 再把twoSubject中的值放到twoFinalSubjectsList中
                    twoFinalSubjectsList.add(twoSubject);
                }
            }
            // 把一级下面所有二级分类放到一级分类中
            oneSubject.setChildren(twoFinalSubjectsList);
        }
        return finalSubjectsList;
      }*/

}
